﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FOModels.Articles;
using Bussiness.Articles;
using Bussiness.Language;
using FOModels.Language;
using System.Net;
using FOSystem.CultureHelper;
namespace FO.Controllers
{
    public class ArticlesController : CultureController
    {
        //
        // GET: /Articles/

        public ActionResult Index()
        {
            return View();
        }
        [Authorize(Roles = "8,9")]
        public ActionResult ManagerArticles()
        {
            return View();
        }
        public ActionResult ArticlesDetail(int ArticlesID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ArticlesBLL cardbll = new ArticlesBLL();
            ArticlesModels list = new ArticlesModels();
            list = cardbll.getArticlesByIDShow(ArticlesID,lang.LangID);
            return View(list);
        }
        public JsonResult getArticlesJson()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ArticlesBLL cardbll = new ArticlesBLL();
            List<ArticlesModels> list = new List<ArticlesModels>();
            list = cardbll.getall(lang.LangID);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertArticles()
        {
            ArticlesBLL cardbll = new ArticlesBLL();
            List<ArticlesCateModels> list = new List<ArticlesCateModels>();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            list = cardbll.getArticlesCate(lang.LangID);
            ViewBag.ArticlesCateListID = new SelectList(list, "ArticlesCateID", "ArticlesCateName");
            return View();
        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertArticles(FO.Models.ArticlesInsert card)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ArticlesBLL cardbll = new ArticlesBLL();
            List<ArticlesCateModels> list = new List<ArticlesCateModels>();
            list = cardbll.getArticlesCate(lang.LangID);
            ViewBag.ArticlesCateListID = new SelectList(list, "ArticlesCateID", "ArticlesCateName");
            if (ModelState.IsValid)
            {
                ArticlesModels items = new ArticlesModels();
                ArticlesModels itemsLang = new ArticlesModels();
                items.Images = card.Images;
                items.UserPost = User.Identity.Name;
                items.Display = false;
                items.ArticlesCateID = card.ArticlesCateID;
                items.Title = card.Title;
                items.Summary = card.Summary;
                items.Content = WebUtility.HtmlEncode(card.Content);
                items.Display = false;
                items.DatePost = DateTime.Now.ToString();
                ////
                itemsLang.Images = card.Images;
                itemsLang.UserPost = User.Identity.Name;
                itemsLang.Display = false;
                itemsLang.ArticlesCateID = card.ArticlesCateID;
                itemsLang.Title = card.TitleLang;
                itemsLang.Summary = card.SummaryLang;
                itemsLang.Content = WebUtility.HtmlEncode(card.ContentLang);
                itemsLang.Display = false;
                itemsLang.DatePost = DateTime.Now.ToString();
                cardbll.InsertArticles(items);
                ArticlesModels carditem = cardbll.getTOP1();
                items.ArticlesID = carditem.ArticlesID;
                itemsLang.ArticlesID = carditem.ArticlesID;
                cardbll.InsertArticlesLanng(items, 1);
                cardbll.InsertArticlesLanng(itemsLang, 2);
                return RedirectToAction("ManagerArticles");
            }
            return View(card);
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateArticles(int ArticlesID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ArticlesBLL cardbll = new ArticlesBLL();
            List<ArticlesCateModels> list = new List<ArticlesCateModels>();
            list = cardbll.getArticlesCate(lang.LangID);
            Models.Articles c = new Models.Articles();
            ArticlesModels card = cardbll.getArticlesByID(ArticlesID, lang.LangID);
            ViewBag.ArticlesCateListID = new SelectList(list, "ArticlesCateID", "ArticlesCateName", card.ArticlesCateID);
            c.Images = card.Images;
            c.ArticlesID = card.ArticlesID;
            c.DateUpdate = card.DateUpdate;
            c.UserUpdate = card.UserUpdate;
            c.DatePost = card.DatePost;
            c.UserPost = card.UserPost;
            c.Display = card.Display;
            c.Title = card.Title;
            c.Summary = card.Summary;
            c.ArticlesCateID = card.ArticlesCateID;
            c.Content = WebUtility.HtmlDecode(card.Content);
            return View(c);
        }
        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateArticles(FO.Models.Articles card)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ArticlesBLL cardbll = new ArticlesBLL();
            List<ArticlesCateModels> list = new List<ArticlesCateModels>();
            ArticlesModels ca = new ArticlesModels();

            list = cardbll.getArticlesCate(lang.LangID);
            ca = cardbll.getArticlesByID(card.ArticlesID, lang.LangID);
            ViewBag.ArticlesCateListID = new SelectList(list, "ArticlesCateID", "ArticlesCateName", ca.ArticlesID);
            if (ca != null)
            {
                ca.Images = card.Images;
                ca.ArticlesID = card.ArticlesID;
                ca.DateUpdate = card.DateUpdate;
                ca.UserUpdate = User.Identity.Name;
                ca.DateUpdate = DateTime.Now.ToString();
                ca.Display = card.Display;
                ca.Title = card.Title;
                ca.Summary = card.Summary;
                ca.ArticlesCateID = card.ArticlesCateID;
                ca.Content = WebUtility.HtmlEncode(card.Content);
                cardbll.UpdateArticles(ca, lang.LangID);

                return RedirectToAction("ManagerArticles");
            }
            return View();


        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteArticles(int ArticlesID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ArticlesBLL bll = new ArticlesBLL();
            ArticlesModels card = new ArticlesModels();
            card = bll.getArticlesByID(ArticlesID, lang.LangID);
            if (card != null)
            {
                bll.DeleteArticles(card.ArticlesID);
                var result = new { Status = "OK" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Status = "Xóa thất bại" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }


        }
    }
}
